package com.G_CN06.commonUtil.commonResultConfig;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hcx
 * @className R
 * @description 后端统一返回数据格式
 * @date 2023/2/8 13:49
 **/

/**
  * 数据格式CommonResult:{
 *      success: true,
 *      code: 20000,
 *      message: "登录成功"
 *      data: {
 *          具体数据，key value形式
 *      }
 *  }
 **/


@Data
public class CommonResult {

    @ApiModelProperty(value ="是否成功")
    private boolean success;

    @ApiModelProperty(value ="返回码")
    private Integer code;//20000 成功,50010 未登录

    @ApiModelProperty(value ="返回消息")
    private String message;

    @ApiModelProperty(value ="返回数据")
    private Map<String,Object> data = new HashMap<>();

    public CommonResult(){}

    public CommonResult ok(){
        this.setSuccess(true);
        this.setCode(20000);
        this.setMessage("成功");
        return this;
    }
    public CommonResult ok(String key,Object data){
        this.setSuccess(true);
        this.setCode(20000);
        this.setMessage("成功");
        this.data(key,data);
        return this;
    }

    public CommonResult error(Integer code){
        this.setSuccess(false);
        this.setCode(code);
        this.setMessage("失败");
        return this;
    }
    public CommonResult error(){
        this.setSuccess(false);
        this.setCode(20001);
        this.setMessage("失败");
        return this;
    }

    public CommonResult data(Map<String,Object> map){
        this.data = map;
        return this;
    }

    public CommonResult data(String key,Object value){
        this.data.put(key,value);
        return this;
    }

    public CommonResult message(String message){
        this.message = message;
        return this;
    }
}
